package com.dailyblue.java.io;

import java.io.*;
import java.util.Scanner;

// 带缓冲的效果
public class DemoG {
    private static Scanner scan = new Scanner(System.in);

    // 不带缓冲的
    private static void a1() throws IOException {
        File file = new File("D://a//a//b.txt");
        OutputStream out = new FileOutputStream(file);
        while (true) {
            System.out.print("Input:");
            String s = scan.next();
            if ("exit".equals(s)) {
                break;
            }
            // 将内容写入到硬盘中
            out.write((s + "\n").getBytes());
        }
    }

    // 带缓冲流的
    private static void a2() throws IOException {
        File file = new File("D://a//a//b.txt");
        OutputStream out = new FileOutputStream(file);
        // 缓冲操作
        BufferedOutputStream bos = new BufferedOutputStream(out);
        while (true) {
            System.out.print("Input:");
            String s = scan.next();
            if ("exit".equals(s)) {
                break;
            }
            // 将内容写入到缓冲区中
            bos.write((s + "\n").getBytes());
        }
        // 刷新缓冲区 将缓冲区的内容写入到硬盘中
        bos.flush();
        // 关闭时自带一次刷新
        bos.close();
    }

    // 带缓冲的读取操作
    private static void b() throws IOException{
        InputStream in = new FileInputStream("D://a//a//a.txt");
        BufferedInputStream bis = new BufferedInputStream(in);
        byte[] bytes = new byte[10];
        int len = bis.read(bytes);
        String str = new String(bytes,0,len);
        System.out.println(str);
    }

    public static void main(String[] args) throws IOException {
        b();
    }
}
